package com.ctrl.riveradministrator.mvp.view.activity.river_policy;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.RiverInfo;
import com.ctrl.riveradministrator.mvp.model.RiverPolicy;
import com.ctrl.riveradministrator.mvp.presenter.RiverPolicyPresenter;
import com.ctrl.riveradministrator.mvp.view.activity.river_info.RiverInfoView;

import es.voghdev.pdfviewpager.library.PDFViewPager;
import es.voghdev.pdfviewpager.library.RemotePDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;
import es.voghdev.pdfviewpager.library.remote.DownloadFile;
import es.voghdev.pdfviewpager.library.util.FileUtil;

public class RiverPolicyActivity extends BaseActivity<RiverPolicyPresenter> implements DownloadFile.Listener
        , RiverPolicyView {

    private Toolbar toolbar;
    private TextView titleTv;
    private RiverInfo riverInfo;
    private PDFViewPager pdfViewPager;
    private RemotePDFViewPager remotePDFViewPager;
    private ImageView leftMenu;
    private PDFPagerAdapter adapter;
    private ConstraintLayout content;
    private LinearLayout notFound;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_river_policy;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        riverInfo = (RiverInfo) getIntent().getSerializableExtra("river_info");
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        content = $(R.id.content);
        pdfViewPager = $(R.id.pdf);
        leftMenu = $(R.id.left_iv);
        notFound = $(R.id.not_found);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(riverInfo.getRiverName());
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        mPresenter.getRiverPolicy(riverInfo.getId());
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                this.finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    public void showMessage(RiverPolicy riverPolicy) {
        try {
            remotePDFViewPager = new RemotePDFViewPager(this, Constants.BASE_URL_ + riverPolicy.getPath(), this);
            remotePDFViewPager.setId(R.id.pdf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected RiverPolicyPresenter createPresenter() {
        return new RiverPolicyPresenter(this);
    }


    @Override
    public void onSuccess(String url, String destinationPath) {
        adapter = new PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url));
        remotePDFViewPager.setAdapter(adapter);
        content.removeAllViewsInLayout();
        content.addView(remotePDFViewPager, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onFailure(Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProgressUpdate(int progress, int total) {

    }
}
